﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Santa_sAdventure.Model
{
    class ShootingEnemy : Enemy
    {
        // Enemy specific animation
        private Animation m_idleAnimation;
        private Animation m_shootAnimation;
        private Animation m_die;
        private AnimationPlayer m_animationPlayer;
        private SpriteEffects flip = SpriteEffects.None;
        // Content
        private ContentManager m_content;
        // Cooldown after every shot
        private int m_coolDown = 2;
        private float m_timeElapsed = 0;
        // Direction, -1 = left 1 = right
        private int m_direction;
        // Bullets
        private List<Bullet> m_bulletList = new List<Bullet>();
        // Sound
        private SoundEffect m_shotSound;
        private SoundEffectInstance m_shotSoundInstance;


        internal ShootingEnemy(ContentManager a_content, int a_direction, int a_seed)
        {
            m_content = a_content;
            Velocity = new Vector2(0, 0);
            m_direction = a_direction;

            // Set animations
            m_idleAnimation = new Animation(a_content.Load<Texture2D>("Images/Enemy/Shooting/Idle-shootingenemy"), 2, true, 1);
            m_shootAnimation = new Animation(a_content.Load<Texture2D>("Images/Enemy/Shooting/Enemy-Shooting"), 2, true, 10);
            m_die = new Animation(a_content.Load<Texture2D>("Images/Enemy/Enemy-Die"), 1, false, 11);
            m_animationPlayer = new AnimationPlayer(m_idleAnimation);

            Random rand = new Random(a_seed);
            m_coolDown = rand.Next(0,3);

            // Sound
            m_shotSound = a_content.Load<SoundEffect>("Sounds/Events/gun-shot");
            m_shotSoundInstance = m_shotSound.CreateInstance();
            m_shotSoundInstance.Volume = 0.3f;
        }

        // Update
        internal override void Update(float a_timeElapsedInSec)
        {
            // Shooting
            m_timeElapsed += a_timeElapsedInSec;
            // If we can shoot
            if (m_timeElapsed > m_coolDown)
            {
                // First time reset
                if (m_coolDown != 2)
                {
                    m_coolDown = 2;
                }
                // Reset time
                m_timeElapsed = 0;
                // Shoot bullet
                Vector2 bulletVelocity;
                Vector2 bulletPosition;
                if (m_direction > 0)
	            {
                    // Looking right
                    bulletVelocity = new Vector2(5, 0);
                    bulletPosition = new Vector2(Position.X + 1, Position.Y + 1);
	            }
                else
	            {
                    // Looking left
                    bulletVelocity = new Vector2(-5, 0);
                    bulletPosition = new Vector2(Position.X, Position.Y + 1);
	            }
                // Shoot a new bullet
                Bullet bullet = new Bullet(new Vector2(bulletPosition.X, bulletPosition.Y), bulletVelocity);
                m_bulletList.Add(bullet);
                m_shotSoundInstance.Play();
                m_animationPlayer.PlayAnimation(m_shootAnimation);
            }
        }

        internal void UpdateBullets(float a_timeElapsedInSec)
        {
            // Update all bullets
            for (int i = 0; i < m_bulletList.Count; i++)
            {
                m_bulletList[i].Update(a_timeElapsedInSec);
            }
        }

        // Returns all the bullets
        internal List<Bullet> GetAllBullets { get { return m_bulletList; } }

        // Kill specific bulet
        internal void KillBullet(int a_index)
        {
            m_bulletList.RemoveAt(a_index);
        }

        // Return animationplayer
        internal override AnimationPlayer GetAnimationPlayer()
        {
            return m_animationPlayer;
        }

        // Return spriteeffects
        internal override SpriteEffects GetSpriteEffect()
        {
            // Flip the sprite to face the way we are moving.
            if (m_direction > 0)
                flip = SpriteEffects.None;
            else if (m_direction < 0)
                flip = SpriteEffects.FlipHorizontally;

            return flip;
        }

        // Kill enemy
        internal override void KillEnemy()
        {
            m_isDead = true;
            m_animationPlayer.PlayAnimation(m_die);
        }
    }
}
